<?php
namespace Umpmobile\Model;
use Think\Model;
class InviteModel{
	public $home_info;
	public $gueset_info;
	public $sid;
	public $catid;
	public function invite(){
		if($this->sid && $this->home_info && $this->gueset_info && $this->catid){
			$SHIP = D("Friendships");
			$SHIP->guest_id=$this->gueset_info["id"];
			$SHIP->home_id=$this->home_info["id"];
			$SHIP->sid=$this->sid;
			$SHIP->promo_cat_id=$this->catid;
			$ship = $SHIP->getFriendShip();
			
			$PROM = D("Promosions");			
			if($ship){
				// 老朋友直接读取当前折扣
				$PROM->my_nick = $this->home_info["mixed_nick"]; 
				$PROM->friend_info = $this->gueset_info["mixed_nick"]; 
				$PROM->sid = $this->sid;
				$PROM->cat_id = $this->catid;
				$cur_prom = $PROM->getCurProm();
			}else{
				$this->gueset_info["friends"]++; //邀请我的人，朋友数量加1
				
				$FRI = D("Friends");
				$FRI->sid=$this->sid;
				$FRI->id = $this->gueset_info["id"];
				$FRI->friendsUp();//朋友数量增加写入到数据库
				
				//准备存入朋友关系。朋友链的主客关系刚好相反
				$SHIP->home_mixednick = $this->gueset_info["mixed_nick"]; 
				$SHIP->guest_mixednick = $this->home_info["mixed_nick"]; 
				$SHIP->invite_order=$this->gueset_info["friends"]; //邀请顺序
				$SHIP->promo_cat_id=$this->catid;
				$ship = $SHIP->addFriendShip(); //朋友关系增加
			
				// 新朋友，朋友数量增加后再读取当前折扣
				$PROM->my_nick = $this->gueset_info["mixed_nick"]; 
				$PROM->friend_info = $this->gueset_info; 
				$PROM->sid = $this->sid;
				$PROM->cat_id = $this->catid;
				$cur_prom = $PROM->getCurProm(); //取出合适的折扣，准备打折
				
				if($this->gueset_info["friends"]==$cur_prom["friend_num"]){
					//记录最后一次获得折扣的ID和减钱数量；
					$FRI->id = $this->gueset_info["id"]; 
					$FRI->last_promo_id = $cur_prom["id"];
					$FRI->last_decrease_amount = $cur_prom["decrease_amount"];
					$FRI->lastPromSave();
					
					$PROM->cur_prom = $cur_prom;
					$PROM->my_nick = $this->gueset_info["mixed_nick"]; //给从URL获取的昵称打折
					$re = $PROM->addUserTag();
					
					if($re){
						// 成功数据写入朋友链
						$data["activity_id"]=$cur_prom["activity_id"];
						$data["promo_id"]=$cur_prom["id"];
						$SHIP->where("`id`=".$ship)->save($data);
					}
					$cur_prom["is_invite"] = $re;
				}
			}
			return $cur_prom;
		}else{
			return false;
		}
	}
	public function get_code_img(){
		$friend = $this->home_info;
		if($friend["qrcode_url"]){
			$resp = new \stdClass();
			$resp->modules->qrcode_d_o->qrcode_url = $friend["qrcode_url"];
			$resp->modules->qrcode_d_o->url = $friend["friend_link"];
			$resp->modules->qrcode_d_o->short_url = $friend["short_url"];
			$resp->suc=true;
		}else{
			Vendor("taobaosdk.top.TopClient");
			Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
			Vendor("taobaosdk.top.RequestCheckUtil");
			Vendor("taobaosdk.top.request.MaQrcodeCommonCreateRequest");
			Vendor("taobaosdk.top.request.WirelessXcodeCreateRequest");
			$c = new \TopClient;
			$c->appkey = C("SM_KEY");//用后台的key
			$c->secretKey = C("SM_SECRET"); 
			$req = new \MaQrcodeCommonCreateRequest;
			$req->setType("url");
			$req->setContent("https://hongbao.ews.m.jaeapp.com/?friendnick=".urlencode($this->home_info["mixed_nick"])."&sid=".$this->sid."&cid=".$this->catid);
			$req->setName("淘小店的无线店铺首页");
			$req->setStyle("EF4F2B");
			$req->setSize("300");
			$req->setNeedEps("false");
			$req->setLogo("http://img01.taobaocdn.com/imgextra/T1Od8YFT8eXXXXXXXX");

			$SHOP = M("Shop");
			$sessionKey = $SHOP->where("`sid`=".$this->sid)->getField("sessionkey");

			$resp = $c->execute($req, $sessionKey);
			if($resp->suc && $this->home_info["id"]){
				$friend["qrcode_url"] = (String)$resp->modules->qrcode_d_o->qrcode_url;
				$friend["short_url"] = (String)$resp->modules->qrcode_d_o->short_url;
				$friend["eps_url"] = (String)$resp->modules->qrcode_d_o->eps_url;
				$friend["channel_id"] = (String)$resp->modules->qrcode_d_o->channel_id;
				$friend["channel_name"] = (String)$resp->modules->qrcode_d_o->channel_name;
				$FRI = D("Friends");
				$FRI->friend_id = $this->home_info["id"]; 
				$FRI->save_data = $friend;
				$FRI->saveFriend();
			}
		}
		$resp = json_decode(json_encode($resp),true);
		return ($resp);
	}
}